package com.detrum.sportcamera.runnable;

import android.os.Handler;
import android.util.Log;

import com.detrum.sportcamera.finals.NfsNetFinal;
import com.detrum.utils.LogUtils;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketTimeoutException;
import java.util.Arrays;

/**
 * Created by abc on 2018/2/3.
 */

public class MsgRunnable implements Runnable {
    private DatagramSocket sendSocket;
    private Handler handler;
    private ResponseCallBack callBack;

    public MsgRunnable(Handler handler,DatagramSocket sendSocket, ResponseCallBack callBack) {
        this.handler = handler;
        this.callBack = callBack;
        this.sendSocket = sendSocket;
    }

    /**
     * 回调回主线程
     */
    private void handleSuccess(final ResponseCallBack callBack, final ResponseCallBack.ActionTypes actionTypes, final byte[] response) {
        if (callBack != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    callBack.succeed(actionTypes, response);
                }
            });
        }
    }

    /**
     * 回调回主线程
     */
    private void handleFailure(final ResponseCallBack callBack, final ResponseCallBack.ActionTypes actionTypes, final byte[] response) {
        if (callBack != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    callBack.failed(actionTypes, response);
                }
            });
        }
    }
    @Override
    public void run() {
        while (true) {
            byte[] receiveData = new byte[1024];
            DatagramPacket outPacket = new DatagramPacket(receiveData, receiveData.length);


                if (sendSocket != null&&!sendSocket.isClosed()) {
                    try {
                        sendSocket.receive(outPacket);
                    } catch (IOException e) {
                        //handler.sendEmptyMessage(200);
                        e.printStackTrace();
                    }
                }else{
                    return;
                }

            if (outPacket != null && outPacket.getLength()!= 1024) {
                byte[] response;
                response = outPacket.getData();

                if (callBack != null) {
                    switch (response[11]) {
                        case NfsNetFinal.LOGIN + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.LOGIN, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.LOGIN, response);
                            }
                            break;
                        case NfsNetFinal.LOGOUT + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.LOGOUT, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.LOGOUT, response);
                            }
                            break;
                        case NfsNetFinal.HEART:
                            //心跳不做处理
                            break;
                        case NfsNetFinal.SETTIME:
                            //设置时间不做处理
                            break;
                        case NfsNetFinal.FW_VERSION://返回版本信息
                            String str = Arrays.toString(response);
                            //LogUtils.e(" wifi version = "+str);
                            handleSuccess(callBack, ResponseCallBack.ActionTypes.FW_VERSION, response);
                            break;
                        case NfsNetFinal.CUR_STATUS://返回设备状态信息，是否正在录像
                            String str1 = Arrays.toString(response);
                           // LogUtils.e(" wifi status = "+str1);
                            handleSuccess(callBack, ResponseCallBack.ActionTypes.CUR_STATUS, response);
                            break;
                        case NfsNetFinal.BAT_REMAIN:
                            handleSuccess(callBack, ResponseCallBack.ActionTypes.BAT_REMAIN, response);
                            break;
                        case NfsNetFinal.START_RTP + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.START_RTP, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.START_RTP, response);
                            }
                            break;
                        case NfsNetFinal.STOP_RTP + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.STOP_RTP, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.STOP_RTP, response);
                            }
                            break;
                        case NfsNetFinal.SET_RECORD_POLICY + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.SET_RECORD_POLICY, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.SET_RECORD_POLICY, response);
                            }
                            break;
                        case NfsNetFinal.GET_RECORD_POLICY + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.GET_RECORD_POLICY, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.GET_RECORD_POLICY, response);
                            }
                            break;
                        case NfsNetFinal.SET_RECORD_QUALITY+1:
                            if(response[12]==0){
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.SET_RECORD_QUALITY, response);
                            }else{
                                handleFailure(callBack, ResponseCallBack.ActionTypes.SET_RECORD_QUALITY, response);
                            }
                            break;
                        case NfsNetFinal.GET_RECORD_QUALITY+1:
                            if(response[12]==0){
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.GET_RECORD_QUALITY, response);
                            }else{
                                handleFailure(callBack, ResponseCallBack.ActionTypes.GET_RECORD_QUALITY, response);
                            }
                            break;
                        case NfsNetFinal.SET_RECORD_MODE + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.SET_RECORD_MODE, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.SET_RECORD_MODE, response);
                            }
                            break;
                        case NfsNetFinal.GET_RECORD_MODE + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.GET_RECORD_MODE, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.GET_RECORD_MODE, response);
                            }
                            break;
                        case NfsNetFinal.SET_RECORD_RESOLUTION + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.SET_RECORD_RESOLUTION, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.SET_RECORD_RESOLUTION, response);
                            }
                            break;
                        case NfsNetFinal.GET_RECORD_RESOLUTION + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.GET_RECORD_RESOLUTION, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.GET_RECORD_RESOLUTION, response);
                            }
                            break;
                        case NfsNetFinal.START_RECORD + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.START_RECORD, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.START_RECORD, response);
                            }
                            break;
                        case NfsNetFinal.PAUSE_RECORD + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.PAUSE_RECORD, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.PAUSE_RECORD, response);
                            }
                            break;
                        case NfsNetFinal.STOP_RECORD + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.STOP_RECORD, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.STOP_RECORD, response);
                            }
                            break;
                        case NfsNetFinal.PHOTO_RESOLUTION + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.PHOTO_RESOLUTION, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.PHOTO_RESOLUTION, response);
                            }
                            break;
                        case NfsNetFinal.TAKE_PHOTO + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.TAKE_PHOTO, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.TAKE_PHOTO, response);
                            }
                            break;
                        case NfsNetFinal.SET_WB_MODE + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.SET_WB_MODE, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.SET_WB_MODE, response);
                            }
                            break;
                        case NfsNetFinal.GET_WB_MODE + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.GET_WB_MODE, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.GET_WB_MODE, response);
                            }
                            break;
                        case NfsNetFinal.SET_EXPOSURE_MODE + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.SET_EXPOSURE_MODE, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.SET_EXPOSURE_MODE, response);
                            }
                            break;
                        case NfsNetFinal.GET_EXPOSURE_MODE + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.GET_EXPOSURE_MODE, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.GET_EXPOSURE_MODE, response);
                            }
                            break;
                        case (byte) (NfsNetFinal.D_CAM_GET_TV_MODE)+1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.GET_TV_MODE, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.GET_TV_MODE, response);
                            }
                            break;
                        case (byte) NfsNetFinal.D_CAM_SET_TV_MODE+1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.SET_TV_MODE, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.SET_TV_MODE, response);
                            }
                            break;

                        case NfsNetFinal.D_CAM_GET_OSD+1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.GET_OSD_MODE, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.GET_OSD_MODE, response);
                            }
                            break;
                        case NfsNetFinal.D_CAM_SET_OSD+1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.SET_OSD_MODE, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.SET_OSD_MODE, response);
                            }
                            break;
                        case (byte) NfsNetFinal.GET_SD_INFO + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.GET_SD_INFO, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.GET_SD_INFO, response);
                            }
                            break;
                        case (byte) NfsNetFinal.RECOVERY_DEFAULT_SET + 1:
                            if (response[12] == 0) {                // 信息返回帧第13位表示错误码，0表示成功返回
                                handleSuccess(callBack, ResponseCallBack.ActionTypes.RECOVERY_DEFAULT_SET, response);
                            } else {
                                handleFailure(callBack, ResponseCallBack.ActionTypes.RECOVERY_DEFAULT_SET, response);
                            }
                            break;
                        case NfsNetFinal.PHOTO_NAME:
                            handleSuccess(callBack,ResponseCallBack.ActionTypes.PHOTO_NAME,response);
                            break;
                        case NfsNetFinal.VIDEO_NAME:
                            handleSuccess(callBack,ResponseCallBack.ActionTypes.VIDEO_NAME,response);
                            break;
                        case (byte)NfsNetFinal.D_CAM_DO_UPGRADE+1:
                            LogUtils.e("response[11] ---------- "+response[11]);
                            LogUtils.e("response[12] ----------  "+response[12]);
                            if(response[12]==0){
                                handleSuccess(callBack,ResponseCallBack.ActionTypes.D_CAM_DO_UPGRADE,response);
                            }else{
                                handleFailure(callBack, ResponseCallBack.ActionTypes.D_CAM_DO_UPGRADE, response);
                            }
                            break;
                    }
                }else{

                }
            }
        }
    }
}
